<?php
apf_require_class("Solr_Dao_Base");
class Solr_Dao_App extends Solr_Dao_Base{

    public function insert_application($host_id,$webapp_id,$memo){
        $pdo = $this->get_srm_pdo();
        $sql = "insert into application set host_id=?,webapp_id=?,memo=? ";
        $stmt = $pdo->prepare($sql);
        $stmt->execute(array($host_id,$webapp_id,$memo));
        return $pdo->lastInsertId();
    }

    public function get_apps_list($hostname_id,$port) {
        $pdo = $this->get_srs_pdo();
        $where='';
        if($hostname_id)$where.=' and a.host_id='.$hostname_id;
        if($port)$where.=' and c.port='.$port;
        $sql = "select a.*,b.hostname,b.color,c.solrname,c.webapp,c.port
        		from application a,hostname b,webapp c
        		where a.host_id=b.id and a.webapp_id=c.id and a.status=1 $where
        		order by b.hostname";
        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll();
    }

    public function get_idx_port_list(){
        $pdo = $this->get_srs_pdo();
        $sql = "select a.host_id,b.hostname,c.port
        		from application a,hostname b,webapp c
        		where a.host_id=b.id and a.webapp_id=c.id and a.status=1
        		group by hostname,port";
        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll();
    }

    public function insert_hostname($hostname){
        $pdo = $this->get_srm_pdo();
        $sql = "insert into hostname set hostname=? ";
        $stmt = $pdo->prepare($sql);
        $stmt->execute(array($hostname));
        return $pdo->lastInsertId();
    }

    public function get_all_hostname(){
        $pdo = $this->get_srs_pdo();
        $sql = "select * from hostname order by hostname";
        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll();
    }

    public function insert_webapp($webapp,$solrname,$port){
        $pdo = $this->get_srm_pdo();
        $sql = "insert into webapp set webapp=?,solrname=?,port=? ";
        $stmt = $pdo->prepare($sql);
        $stmt->execute(array($webapp,$solrname,$port));
        return $pdo->lastInsertId();
    }

    public function get_all_webapp(){
        $pdo = $this->get_srs_pdo();
        $sql = "select * from webapp order by solrname";
        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll();
    }

    public function get_port_list(){
        $pdo = $this->get_srs_pdo();
        $sql = "select port from webapp group by port";
        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll();
    }

}